package com.bkgy.controller.allprocess.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bkgy.controller.allprocess.dto.AllpricessIndexProductReasonResponse;
import com.bkgy.controller.allprocess.dto.AllpricessIndexReasonRequst;
import com.bkgy.controller.allprocess.mapper.AllProcessIndexProductReasonMapper;
import com.bkgy.controller.allprocess.service.AllProcessIndexProductReasonService;
import com.bkgy.sys.common.entity.ResultData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;


@Service
public class AllProcessIndexProductReasonServiceImpl implements AllProcessIndexProductReasonService {

    @Autowired
    private AllProcessIndexProductReasonMapper productReasonMapper;

     /**
     * 全流程成材率模型首页 产品品种异常原因
     * @param request_get AllpricessIndexReasonRequst
     * @return AllpricessIndexProductReasonResponse
     */
    public  ResultData<AllpricessIndexProductReasonResponse> productReason (AllpricessIndexReasonRequst request_get) {
        //日期转换与赋值
        AllpricessIndexReasonRequst request = setdatetime(request_get);
        try {
            Page<?> page = new Page<>(request.getPageIndex(), request.getPageSize());

            //总行数数
            int count = 0;

            //产品品种 和 异常原因
            List<AllpricessIndexProductReasonResponse> productreason = new ArrayList<>();
            if(request.getLinename()==null)
            {
                request.setLinename("cal1630");
            }
//            if(request.getLinename().equals("all"))
//            {
//                results_otherReason= reasonMapper.getotherReasonAll(page, request);
//            }
            else if(request.getLinename().equals("cal1630"))
            {
                productreason= productReasonMapper.productReason1630(page, request);
                count = (int)page.getTotal();
            }
            else if(request.getLinename().equals("cal2150"))
            {
                productreason= productReasonMapper.productReason2150(page, request);
                count = (int)page.getTotal();
            }
            else if(request.getLinename().equals("cgl1870"))
            {
                productreason= productReasonMapper.productReason1870(page, request);
                count = (int)page.getTotal();
            }
            else if(request.getLinename().equals("tcm2280"))
            {
                productreason= productReasonMapper.productReason2280(page, request);
                count = (int)page.getTotal();
            }
            return ResultData.ResultDataSuccess(productreason, count);

        } catch (Exception e) {
            e.printStackTrace();
            return ResultData.ResultDataFaultSelf("获取失败" + e.getMessage(), null);
        }
    }

    /**
     * 根据前台传入查询开始和查询结束日期，构造出三种时间格式以便后续表数据查询
     * @param request_get TbAllpricessIndexEntity
     * @return TbAllpricessIndexEntity
     */
    public AllpricessIndexReasonRequst setdatetime(AllpricessIndexReasonRequst request_get) {
        try {
            String[] array_startTime = request_get.getDbegin().split("-");//时间格式：2025-06-01 00:00:01
            String dbegin_Times = "";//时间格式：20250703075254
            if (array_startTime.length > 0) {
                //构建时间格式：20250703075254
                String times2 = array_startTime[2].replace(" ", "");
                times2 = times2.replace(":", "");
                dbegin_Times = array_startTime[0] + array_startTime[1] + times2;
            }
            request_get.setDbegin(dbegin_Times);//时间格式：20250703075254

            String[] array_endTime = request_get.getDend().split("-");//时间格式：2025-06-01 00:00:01
            String endTime_Times="";//时间格式：20250703075254
            if (array_endTime.length > 0) {
                //构建时间格式：20250703075254
                String times2=array_endTime[2].replace(" ","");
                times2 = times2.replace(":","");
                endTime_Times=array_endTime[0]+array_endTime[1]+times2;
            }
            request_get.setDend(endTime_Times);//时间格式：20250703075254

        } catch (Exception e) {
            e.printStackTrace();
        }
        return request_get;
    }

}